﻿Partial Public Class Log
    <Runtime.CompilerServices.MethodImpl(Runtime.CompilerServices.MethodImplOptions.NoInlining)>
    Friend Shared Sub LogMessage(theMessage As String, Optional theType As LogTypes = LogTypes.Information, Optional theUser As String = Nothing)
        Try
            Using psc As PowerShellCronContext = New PowerShellCronContext()
                Dim cmb As System.Reflection.MethodBase = New StackFrame(1).GetMethod
                psc.Logs.Add(New Log() With {.LogType = theType,
                                             .Message = theMessage,
                                             .Source = String.Format("{0}.{1}", cmb.ReflectedType.ToString, cmb.Name),
                                             .Logged = DateTimeOffset.Now,
                                             .User = IIf(String.IsNullOrEmpty(theUser), System.Security.Principal.WindowsIdentity.GetCurrent.Name, theUser)})
                psc.SaveChanges()
            End Using
        Catch ex As Exception
            IO.File.AppendAllText(DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss.error"), ex.ToString)
        End Try
    End Sub
End Class
